'use strict'
let schedule = require('node-schedule')
let attackHandle = require('../core/attackHandle')
let playerDao = require('../dao/playerDao')
let mailDao = require('../dao/mailDao')
let pushMsg = require('../nodeapp/pushMsg')
let playerSessionMgr = require('../core/playerSessionMgr')

  //试炼之所
class TrialComponent {
  constructor() {
    this.players = new Map()
    this.playersArray = []
  }

  join(player) {
    if (this.players.has(player._id)) {
      return -1
    }

    this.players.set(player._id, player.getPlayerCopy())
    this.playersArray.push(player._id)
    return 0
  }

  hasJoin(player) {
    if (this.players.has(player._id)) {
      return 0
    } else {
      return -1
    }
  }

  getJoinNumber() {
    return this.players.size
  }

  trialBattle() {
    Log('开始试炼.....人数为' + this.playersArray.length)
    if (this.playersArray.length <= 0) {
      return -1
    }

    let players = this.players
    let playersArray = this.playersArray
    this.players = new Map()
    this.playersArray = []

    let self = this
    let length = playersArray.length
    if (length === 1) {
      let playerId = playersArray[0]
      this.reward(playerId, 16)
      pushMsg.trialInfo(playerId, '')
      let p = playerSessionMgr.getPlayer(playerId)
      if (p) {
        p.trialInfo = null
      } else {
        playerDao.updatePlayerById(playerId, {
          trialInfo: null
        }, function(err, res) {})
      }
      return
    }

    for (let i = 0; i < length; i++) {
      let playerId = playersArray[i]
      process.nextTick(function(){
        self.startTrial(playerId, playersArray, players, i)
      })
    }

  }

  startTrial(playerId, playersArray, players, i) {
    let player = players.get(playerId)
    let trialInfo = {
      fightMsgs: [],
      score: 0
    }
    let length = playersArray.length
    for (var j = 0; j < 16; j++) {
      let index = Math.floor(Math.random() * length)
      if (index === i && index === length - 1) {
        index = 0
      } else if (index === i) {
        index += 1
      }
      let battlePlayerId = playersArray[index]
      let battlePlayer = players.get(battlePlayerId)
      let fightMsg = attackHandle.playerBattle(player, battlePlayer)
      trialInfo.fightMsgs.push(fightMsg)
      if (fightMsg.result === 1) {
        trialInfo.score += 1
      }
    }

    this.reward(playerId, trialInfo.score)
    let p = playerSessionMgr.getPlayer(playerId)
    if (p) {
      p.trialInfo = trialInfo
    } else {
      playerDao.updatePlayerById(playerId, {
        trialInfo: trialInfo
      }, function(err, res) {})
    }

    pushMsg.trialInfo(playerId, '')
  }

  //奖励发放
  reward(pId, score) {
    let title = '试炼之所奖励'
    let content = '试炼之所奖励'
    let items = []
    let itemName = ''
    if (score <= 3) {
      items.push([240060, 1, 0])
      itemName = '普通礼包'
    } else if (score <= 6) {
      items.push([240061, 1, 0])
      itemName = '青铜礼包'
    } else if (score <= 10) {
      items.push([240062, 1, 0])
      itemName = '白银礼包'
    } else if (score <= 16) {
      items.push([240063, 1, 0])
      itemName = '黄金礼包'
    }
    mailDao.newAndSave(pId, title, content, items, 0, 0, function(err) {
      pushMsg.newMail(pId, '试炼结束,你获得了' + itemName + ',奖励请到邮箱查看')
    })
  }
}

let trialComponent = new TrialComponent()
module.exports = trialComponent

let rule = new schedule.RecurrenceRule()
rule.minute = [0]
let j = schedule.scheduleJob(rule, function() {
  trialComponent.trialBattle()
})
